<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
</head>
<body>
    <h1>axios的基本使用</h1>
    <button class="tsetget">发送get请求</button>
    <button class="tsetpost">发送post请求</button>
    <button class="tsetput">发送put请求</button>
    <button class="tsetdelete">发送delete请求</button>
    <script>
        //获取按钮
        const btn = document.querySelectorAll('button');

        //get请求
        btn[0].onclick = function(){
            //发送AJAX请求
            axios({
                //请求类型
                method:'GET',
                //URL
                url:'http://localhost:3000/posts/1',
            }).then(v=>{
                console.log(v);
            });
        }
        //post请求
        btn[1].onclick=function(){
            //发送Ajax请求
            axios({
                //请求类型
                method:'POST',
                //url
                url:'http://localhost:3000/posts',
                //设置请求体
                data:{
                    title:"今天天气凉快,还下雨了",
                    author:"张三"
                }
            }).then(v=>{
                console.log(v);
            });
        }

        //put请求
        btn[2].onclick=function(){
            axios({
                method:'PUT',
                url:'http://localhost:3000/posts/2',
                data:{
                    title:"今天下雨了",
                    author:"李四"
                }
            }).then(response=>{
                console.log(response);
            });
        }
        //删除数据
        btn[3].onclick=function(){
            axios({
                method:'DELETE',
                url:'http://localhost:3000/posts/2'
            });
        }
    </script>
</body>
</html>